/*
 * @Author: master
 * @Date: 2022-04-20 17:31:46
 * @LastEditTime: 2022-04-21 13:15:12
 * @Description: 自有vue3.0组件库，组件库style按需引入
 */
import _ from 'lodash';
import glob from 'glob';
import path from 'path';
const root = process.cwd();
const resolvePath = (...relativePath) => {
  return path.resolve(root, ...relativePath);
};
// pc端css的模块文件
const pcFiles = glob.sync('**/*.css', {
  cwd: resolvePath('node_modules/hytd-vue-next/pc'),
});
// mobile端css的模块文件
const mobileFiles = glob.sync('**/*.css', {
  cwd: resolvePath('node_modules/hytd-vue-next/mobile'),
});
/**
 * 获取css路径
 */
const getPath = (files, name, type = 'pc') => {
  if (_.isArray(files)) {
    const path = files.filter(file => file.indexOf(name) > -1)[0];
    if (path) {
      return `hytd-vue-next/${type}/${path}`;
    } else {
      return '';
    }
  } else {
    return '';
  }
};
/**
 * pc端style的样式引入
 */
const hytdVueNextForPcStyleResolve = () => {
  return {
    libraryName: 'hytd-vue-next/pc',
    esModule: true,
    resolveStyle: name => {
      // 获取css路径
      return getPath(pcFiles, name);
    },
  };
};
/**
 * mobile端style的样式引入
 */
const hytdVueNextForMobileStyleResolve = () => {
  return {
    libraryName: 'hytd-vue-next/mobile',
    esModule: true,
    resolveStyle: name => {
      // 获取css路径
      return getPath(mobileFiles, name, 'mobile');
    },
  };
};
/**
 * 默认输出pc端的样式引入
 */
const hytdVueNextStyleResolve = () => {
  return {
    libraryName: 'hytd-vue-next',
    esModule: true,
    resolveStyle: name => {
      // 获取css路径
      return getPath(pcFiles, name);
    },
  };
};
/**
 * 组件库style按需引入
 * @returns Array resolve
 */
export default function hytdStyleResolve() {
  return [hytdVueNextForPcStyleResolve(), hytdVueNextForMobileStyleResolve(), hytdVueNextStyleResolve()];
}
